﻿using Pipes.Xml;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Pipes.Sgml;
using System.IO;
using System.Collections.Generic;
using Pipes.Json;

namespace Pipes.UnitTest
{
  /// <summary>
  ///This is a test class for UtilTest and is intended
  ///to contain all UtilTest Unit Tests
  ///</summary>
  [TestClass()]
  public class XmlUtilsTest
  {


    private TestContext testContextInstance;

    /// <summary>
    ///Gets or sets the test context which provides
    ///information about and functionality for the current test run.
    ///</summary>
    public TestContext TestContext
    {
      get
      {
        return testContextInstance;
      }
      set
      {
        testContextInstance = value;
      }
    }

    #region Additional test attributes
    // 
    //You can use the following additional attributes as you write your tests:
    //
    //Use ClassInitialize to run code before running the first test in the class
    //[ClassInitialize()]
    //public static void MyClassInitialize(TestContext testContext)
    //{
    //}
    //
    //Use ClassCleanup to run code after all tests in a class have run
    //[ClassCleanup()]
    //public static void MyClassCleanup()
    //{
    //}
    //
    //Use TestInitialize to run code before running each test
    //[TestInitialize()]
    //public void MyTestInitialize()
    //{
    //}
    //
    //Use TestCleanup to run code after each test has run
    //[TestCleanup()]
    //public void MyTestCleanup()
    //{
    //}
    //
    #endregion

    /// <summary>
    ///A test for WriteTo
    ///</summary>
    [TestMethod()]
    public void XmlToJson_SimpleTest()
    {
      TestXmlConversion(@"<a><b v=""thing"">value</b><e>2</e><d><e>1</e><g>3</g></d><b>two</b><f v=""thing"" x=""another"">val</f></a>",
        @"{""a"":{""b"":[{""@v"":""thing"",""#text"":""value""},""two""],""e"":""2"",""d"":{""e"":""1"",""g"":""3""},""f"":{""@v"":""thing"",""@x"":""another"",""#text"":""val""}}}");
    }

    [TestMethod()]
    public void XmlToJson_AmlTest()
    {
      TestXmlConversion(@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/""> <SOAP-ENV:Body> <Result> <Item type=""Person"" typeId=""A4AFA282D2F949C8B3B400CB313A634A"" id=""3F728327F68A4F439C36FBB93704E202""> <email>eric.walstra@gentex.com</email> <id keyed_name=""Eric Walstra"" type=""Person"">3F728327F68A4F439C36FBB93704E202</id> <location keyed_name=""Gentex: Centennial"" type=""Site""> <Item type=""Site"" typeId=""52EC3DA140EB49F9A12AE3B344581FA6"" id=""25B7A4B0F9ED4EC593729EDDBEA52D3B""> <id keyed_name=""Gentex: Centennial"" type=""Site"">25B7A4B0F9ED4EC593729EDDBEA52D3B</id> </Item> </location> <manager keyed_name=""Kenneth Horner"" type=""Person"">A628DE16A60248B1939C162022558D66</manager> </Item> <Item type=""Person"" typeId=""A4AFA282D2F949C8B3B400CB313A634A"" id=""13E517A572A54A45B4187657346438C5""> <email is_null=""1"" /> <id keyed_name=""Eric Watson"" type=""Person"">13E517A572A54A45B4187657346438C5</id> <location keyed_name=""Gentex: State"" type=""Site""> <Item type=""Site"" typeId=""52EC3DA140EB49F9A12AE3B344581FA6"" id=""C86DCDBCD44448619DF85061A0A2F762""> <id keyed_name=""Gentex: State"" type=""Site"">C86DCDBCD44448619DF85061A0A2F762</id> </Item> </location> <manager keyed_name=""Robert Vandervennen"" type=""Person"">92A54C30E1994A8DBDFB9654D133CCF3</manager> </Item> <Item type=""Person"" typeId=""A4AFA282D2F949C8B3B400CB313A634A"" id=""C1210AFB342A4B509B4F6B6B6538BCB7""> <email>eric.woodward@gentex.com</email> <id keyed_name=""Eric Woodward"" type=""Person"">C1210AFB342A4B509B4F6B6B6538BCB7</id> <location keyed_name=""Gentex: Centennial"" type=""Site""> <Item type=""Site"" typeId=""52EC3DA140EB49F9A12AE3B344581FA6"" id=""25B7A4B0F9ED4EC593729EDDBEA52D3B""> <id keyed_name=""Gentex: Centennial"" type=""Site"">25B7A4B0F9ED4EC593729EDDBEA52D3B</id> </Item> </location> <manager keyed_name=""Paul Flynn"" type=""Person"">E6B5BE1247894283877EC3D02CA77FDB</manager> </Item> <Item type=""Person"" typeId=""A4AFA282D2F949C8B3B400CB313A634A"" id=""A3F18443E8EF49CCA74DB54410E0F0E0""> <email>eric.woodwyk@gentex.com</email> <id keyed_name=""Eric Woodwyk"" type=""Person"">A3F18443E8EF49CCA74DB54410E0F0E0</id> <location keyed_name=""Gentex: Centennial"" type=""Site""> <Item type=""Site"" typeId=""52EC3DA140EB49F9A12AE3B344581FA6"" id=""25B7A4B0F9ED4EC593729EDDBEA52D3B""> <id keyed_name=""Gentex: Centennial"" type=""Site"">25B7A4B0F9ED4EC593729EDDBEA52D3B</id> </Item> </location> <manager keyed_name=""Sherwood Brown"" type=""Person"">EFF308B60729446CBA0E2EBFCF2957ED</manager> </Item> <Item type=""Person"" typeId=""A4AFA282D2F949C8B3B400CB313A634A"" id=""40A4E99E9B534214B872A6BD448C72B5""> <email is_null=""1"" /> <id keyed_name=""Eric Woolpert"" type=""Person"">40A4E99E9B534214B872A6BD448C72B5</id> <location keyed_name=""Gentex: State"" type=""Site""> <Item type=""Site"" typeId=""52EC3DA140EB49F9A12AE3B344581FA6"" id=""C86DCDBCD44448619DF85061A0A2F762""> <id keyed_name=""Gentex: State"" type=""Site"">C86DCDBCD44448619DF85061A0A2F762</id> </Item> </location> <manager keyed_name=""Scott Kazmer"" type=""Person"">297FA42D2AD14B7A99998CCB1C38A263</manager> </Item> <Item/> </Result> </SOAP-ENV:Body> </SOAP-ENV:Envelope>",
        @"{""SOAP-ENV:Envelope"":{""@xmlns:SOAP-ENV"":""http://schemas.xmlsoap.org/soap/envelope/"",""SOAP-ENV:Body"":{""Result"":{""Item"":[{""@type"":""Person"",""@typeId"":""A4AFA282D2F949C8B3B400CB313A634A"",""@id"":""3F728327F68A4F439C36FBB93704E202"",""email"":""eric.walstra@gentex.com"",""id"":{""@keyed_name"":""Eric Walstra"",""@type"":""Person"",""#text"":""3F728327F68A4F439C36FBB93704E202""},""location"":{""@keyed_name"":""Gentex: Centennial"",""@type"":""Site"",""Item"":{""@type"":""Site"",""@typeId"":""52EC3DA140EB49F9A12AE3B344581FA6"",""@id"":""25B7A4B0F9ED4EC593729EDDBEA52D3B"",""id"":{""@keyed_name"":""Gentex: Centennial"",""@type"":""Site"",""#text"":""25B7A4B0F9ED4EC593729EDDBEA52D3B""}}},""manager"":{""@keyed_name"":""Kenneth Horner"",""@type"":""Person"",""#text"":""A628DE16A60248B1939C162022558D66""}},{""@type"":""Person"",""@typeId"":""A4AFA282D2F949C8B3B400CB313A634A"",""@id"":""13E517A572A54A45B4187657346438C5"",""email"":{""@is_null"":""1""},""id"":{""@keyed_name"":""Eric Watson"",""@type"":""Person"",""#text"":""13E517A572A54A45B4187657346438C5""},""location"":{""@keyed_name"":""Gentex: State"",""@type"":""Site"",""Item"":{""@type"":""Site"",""@typeId"":""52EC3DA140EB49F9A12AE3B344581FA6"",""@id"":""C86DCDBCD44448619DF85061A0A2F762"",""id"":{""@keyed_name"":""Gentex: State"",""@type"":""Site"",""#text"":""C86DCDBCD44448619DF85061A0A2F762""}}},""manager"":{""@keyed_name"":""Robert Vandervennen"",""@type"":""Person"",""#text"":""92A54C30E1994A8DBDFB9654D133CCF3""}},{""@type"":""Person"",""@typeId"":""A4AFA282D2F949C8B3B400CB313A634A"",""@id"":""C1210AFB342A4B509B4F6B6B6538BCB7"",""email"":""eric.woodward@gentex.com"",""id"":{""@keyed_name"":""Eric Woodward"",""@type"":""Person"",""#text"":""C1210AFB342A4B509B4F6B6B6538BCB7""},""location"":{""@keyed_name"":""Gentex: Centennial"",""@type"":""Site"",""Item"":{""@type"":""Site"",""@typeId"":""52EC3DA140EB49F9A12AE3B344581FA6"",""@id"":""25B7A4B0F9ED4EC593729EDDBEA52D3B"",""id"":{""@keyed_name"":""Gentex: Centennial"",""@type"":""Site"",""#text"":""25B7A4B0F9ED4EC593729EDDBEA52D3B""}}},""manager"":{""@keyed_name"":""Paul Flynn"",""@type"":""Person"",""#text"":""E6B5BE1247894283877EC3D02CA77FDB""}},{""@type"":""Person"",""@typeId"":""A4AFA282D2F949C8B3B400CB313A634A"",""@id"":""A3F18443E8EF49CCA74DB54410E0F0E0"",""email"":""eric.woodwyk@gentex.com"",""id"":{""@keyed_name"":""Eric Woodwyk"",""@type"":""Person"",""#text"":""A3F18443E8EF49CCA74DB54410E0F0E0""},""location"":{""@keyed_name"":""Gentex: Centennial"",""@type"":""Site"",""Item"":{""@type"":""Site"",""@typeId"":""52EC3DA140EB49F9A12AE3B344581FA6"",""@id"":""25B7A4B0F9ED4EC593729EDDBEA52D3B"",""id"":{""@keyed_name"":""Gentex: Centennial"",""@type"":""Site"",""#text"":""25B7A4B0F9ED4EC593729EDDBEA52D3B""}}},""manager"":{""@keyed_name"":""Sherwood Brown"",""@type"":""Person"",""#text"":""EFF308B60729446CBA0E2EBFCF2957ED""}},{""@type"":""Person"",""@typeId"":""A4AFA282D2F949C8B3B400CB313A634A"",""@id"":""40A4E99E9B534214B872A6BD448C72B5"",""email"":{""@is_null"":""1""},""id"":{""@keyed_name"":""Eric Woolpert"",""@type"":""Person"",""#text"":""40A4E99E9B534214B872A6BD448C72B5""},""location"":{""@keyed_name"":""Gentex: State"",""@type"":""Site"",""Item"":{""@type"":""Site"",""@typeId"":""52EC3DA140EB49F9A12AE3B344581FA6"",""@id"":""C86DCDBCD44448619DF85061A0A2F762"",""id"":{""@keyed_name"":""Gentex: State"",""@type"":""Site"",""#text"":""C86DCDBCD44448619DF85061A0A2F762""}}},""manager"":{""@keyed_name"":""Scott Kazmer"",""@type"":""Person"",""#text"":""297FA42D2AD14B7A99998CCB1C38A263""}},null]}}}}");
    }

    void TestXmlConversion(string input, string expected)
    {
      var elem = Xml.Util.GetXmlElement(input);
      var writer = new StringWriter();
      XmlJsonWriter xmlWriter = new XmlJsonWriter(new Pipes.Json.JsonTextWriter(writer));
      elem.WriteTo(xmlWriter);
      xmlWriter.Flush();

      Assert.AreEqual(expected, writer.ToString());
    }

    /// <summary>
    ///A test for XPathSelect
    ///</summary>
    [TestMethod()]
    public void XPathSelectTest_Xml()
    {
      var testDoc = "<?xml version=\"1.0\"?><bookstore specialty=\"novel\">  <book style=\"autobiography\">    <author>      <first-name>Joe</first-name>      <last-name>Bob</last-name>      <award>Trenton Literary Review Honorable Mention</award>    </author>    <price>12</price>  </book>  <book style=\"textbook\">    <author>      <first-name>Mary</first-name>      <last-name>Bob</last-name>      <publication>Selected Short Stories of        <first-name>Mary</first-name>        <last-name>Bob</last-name>      </publication>    </author>    <editor>      <first-name>Britney</first-name>      <last-name>Bob</last-name>    </editor>    <price>55</price>  </book>  <magazine style=\"glossy\" frequency=\"monthly\">    <price>2.50</price>    <subscription price=\"24\" per=\"year\"/>  </magazine>  <book style=\"novel\" id=\"myfave\">    <author>      <first-name>Toni</first-name>      <last-name>Bob</last-name>      <degree from=\"Trenton U\">B.A.</degree>      <degree from=\"Harvard\">Ph.D.</degree>      <award>Pulitzer</award>      <publication>Still in Trenton</publication>      <publication>Trenton Forever</publication>    </author>    <price intl=\"Canada\" exchange=\"0.7\">6.50</price>    <excerpt>      <p>It was a dark and stormy night.</p>      <p>But then all nights in Trenton seem dark and      stormy to someone who has gone through what      <emph>I</emph> have.</p>      <definition-list>        <term>Trenton</term>        <definition>misery</definition>      </definition-list>    </excerpt>  </book>  <my:book xmlns:my=\"uri:mynamespace\" style=\"leather\" price=\"29.50\">    <my:title>Who's Who in Trenton</my:title>    <my:author>Robert Bob</my:author>  </my:book></bookstore>";
      var path = "//book";
      var expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?><Result><book style=\"autobiography\"><author><first-name>Joe</first-name><last-name>Bob</last-name><award>Trenton Literary Review Honorable Mention</award></author><price>12</price></book><book style=\"textbook\"><author><first-name>Mary</first-name><last-name>Bob</last-name><publication>Selected Short Stories of        <first-name>Mary</first-name><last-name>Bob</last-name></publication></author><editor><first-name>Britney</first-name><last-name>Bob</last-name></editor><price>55</price></book><book style=\"novel\" id=\"myfave\"><author><first-name>Toni</first-name><last-name>Bob</last-name><degree from=\"Trenton U\">B.A.</degree><degree from=\"Harvard\">Ph.D.</degree><award>Pulitzer</award><publication>Still in Trenton</publication><publication>Trenton Forever</publication></author><price intl=\"Canada\" exchange=\"0.7\">6.50</price><excerpt><p>It was a dark and stormy night.</p><p>But then all nights in Trenton seem dark and      stormy to someone who has gone through what      <emph>I</emph> have.</p><definition-list><term>Trenton</term><definition>misery</definition></definition-list></excerpt></book></Result>";
      XPathSelectTest(testDoc, path, expected);
    }
    [TestMethod()]
    public void XPathSelectTest_Html()
    {
      var testDoc = @"<!DOCTYPE html><html xmlns:user=""urn:user-scripts"" xmlns:msxsl=""urn:schemas-microsoft-com:xslt"" xmlns:aras=""http://www.aras.com""><head><title>Time Tracking Escalation</title><script type=""text/javascript"">           function OpenItem(type, id, keyedName) {           try {           if (top.aras) {           } else if (window.external) {           }           } catch (ex) {           }           }         </script></head><style type=""text/css"" userData=""Global""> .cellSolid { border-top:1px #000000 solid; border-right:1px #000000 solid; border-bottom:1px #000000 solid; border-left:1px #000000 solid; } .cellSolidTopRight { border-top:1px #000000 solid; border-right:1px #000000 solid; } .cellSolidTopLeft { border-top:1px #000000 solid; border-left:1px #000000 solid; } .cellSolidRightBottom { border-right:1px #000000 solid; border-bottom:1px #000000 solid; } .cellSolidBottomLeft { border-bottom:1px #000000 solid; border-left:1px #000000 solid; } .cellSolidTop { border-top:1px #000000 solid; } .cellSolidRight { border-right:1px #000000 solid; } .cellSolidBottom { border-bottom:1px #000000 solid; } .cellSolidLeft { border-left:1px #000000 solid; } .cellDashed { border-top:1px #666666 dashed; border-right:1px #666666 dashed; border-bottom:1px #666666 dashed; border-left:1px #666666 dashed; } .cellDashedTopRight { border-top:1px #666666 dashed; border-right:1px #666666 dashed; } .cellDashedTopLeft { border-top:1px #666666 dashed; border-left:1px #666666 dashed; } .cellDashedBottomRight { border-bottom:1px #666666 dashed; border-right:1px #666666 dashed; } .cellDashedBottomLeft { border-bottom:1px #666666 dashed; border-left:1px #666666 dashed; } .cellDashedTop { border-top:1px #666666 dashed; } .cellDashedRight { border-right:1px #666666 dashed; } .cellDashedBottom { border-bottom:1px #666666 dashed; } .cellDashedLeft { border-left:1px #666666 dashed; } .cellHeader { background-color:#CCCCCC; border-top:1px #000000 solid; border-right:1px #000000 solid; border-bottom:1px #000000 solid; padding:2px; text-align:center; text-transform:capitalize; text-align:center; font-family:helvetica; font-weight:bold; font-size:8pt; } .cellHeaderVertBT { background-color:#CCCCCC; border-top:1px #000000 solid; border-right:1px #000000 solid; border-bottom:1px #000000 solid; border-left:1px #000000 solid; padding:2px; writing-mode:tb-rl;filter: flipv fliph; text-align:center; text-transform:capitalize; text-align:center; font-family:helvetica; font-weight:bold; font-size:8pt; } .cellHeaderVertTB { background-color:#CCCCCC; border-top:1px #000000 solid; border-right:1px #000000 solid; border-bottom:1px #000000 solid; border-left:1px #000000 solid; padding:2px; writing-mode:tb-rl;filter: flipv flipv; text-align:center; text-transform:capitalize; text-align:center; font-family:helvetica; font-weight:bold; font-size:8pt; } </style><body><div id=""reportBody""><p>Brad             ,             </p><p>The following individuals who report to you are able to log hours in CT+, but have not done so in a while.  Please remind them of the importance of logging hours as it saves Gentex money each year.</p><table><thead><tr><th>Employee</th><th>Last Entry</th></tr></thead><tbody><tr><td>Kevin Schueler</td><td>Jan 02, 2013</td></tr></tbody></table><p>Thanks!</p></div></body></html>";
      var path = "//body";
      var expected = "<body><div id=\"reportBody\"><p>Brad             ,             </p><p>The following individuals who report to you are able to log hours in CT+, but have not done so in a while.  Please remind them of the importance of logging hours as it saves Gentex money each year.</p><table><thead><tr><th>Employee</th><th>Last Entry</th></tr></thead><tbody><tr><td>Kevin Schueler</td><td>Jan 02, 2013</td></tr></tbody></table><p>Thanks!</p></div></body>";
      HtmlXPathSelectTest(testDoc, path, expected);
    }

    private void HtmlXPathSelectTest(string doc, string path, string expected)
    {
      var reader = new Pipes.IO.StringTextSource(doc).Pipe(new Pipes.Sgml.SgmlTextReader());
      var writer = new System.IO.StringWriter();
      var xml = Pipes.Sgml.HtmlTextWriter.Create(writer);
      foreach (var node in reader.XPathSelect(path))
      {
        xml.Node(node);
      }
      xml.Flush();
      Assert.AreEqual(expected, writer.ToString());
    }

    private void XPathSelectTest(string doc, string path, string expected)
    {
      var reader = new Pipes.IO.StringTextSource(doc).Pipe(new Pipes.Xml.XmlTextReader());
      var writer = new System.IO.StringWriter();
      var xml = Pipes.Xml.XmlTextWriter.Create(writer);
      xml.Element("Result");
      foreach (var node in reader.XPathSelect(path))
      {
        xml.Node(node);
      }
      xml.ElementEnd();
      xml.Flush();
      Assert.AreEqual(expected, writer.ToString());
    }

    //"<!DOCTYPE html><html xmlns:user=""urn:user-scripts"" xmlns:msxsl=""urn:schemas-microsoft-com:xslt"" xmlns:aras=""http://www.aras.com""><head><title>Time Tracking Escalation</title><script type=""text/javascript"">           function OpenItem(type, id, keyedName) {           try {           if (top.aras) {           } else if (window.external) {           }           } catch (ex) {           }           }         </script></head><style type=""text/css"" userData=""Global""> .cellSolid { border-top:1px #000000 solid; border-right:1px #000000 solid; border-bottom:1px #000000 solid; border-left:1px #000000 solid; } .cellSolidTopRight { border-top:1px #000000 solid; border-right:1px #000000 solid; } .cellSolidTopLeft { border-top:1px #000000 solid; border-left:1px #000000 solid; } .cellSolidRightBottom { border-right:1px #000000 solid; border-bottom:1px #000000 solid; } .cellSolidBottomLeft { border-bottom:1px #000000 solid; border-left:1px #000000 solid; } .cellSolidTop { border-top:1px #000000 solid; } .cellSolidRight { border-right:1px #000000 solid; } .cellSolidBottom { border-bottom:1px #000000 solid; } .cellSolidLeft { border-left:1px #000000 solid; } .cellDashed { border-top:1px #666666 dashed; border-right:1px #666666 dashed; border-bottom:1px #666666 dashed; border-left:1px #666666 dashed; } .cellDashedTopRight { border-top:1px #666666 dashed; border-right:1px #666666 dashed; } .cellDashedTopLeft { border-top:1px #666666 dashed; border-left:1px #666666 dashed; } .cellDashedBottomRight { border-bottom:1px #666666 dashed; border-right:1px #666666 dashed; } .cellDashedBottomLeft { border-bottom:1px #666666 dashed; border-left:1px #666666 dashed; } .cellDashedTop { border-top:1px #666666 dashed; } .cellDashedRight { border-right:1px #666666 dashed; } .cellDashedBottom { border-bottom:1px #666666 dashed; } .cellDashedLeft { border-left:1px #666666 dashed; } .cellHeader { background-color:#CCCCCC; border-top:1px #000000 solid; border-right:1px #000000 solid; border-bottom:1px #000000 solid; padding:2px; text-align:center; text-transform:capitalize; text-align:center; font-family:helvetica; font-weight:bold; font-size:8pt; } .cellHeaderVertBT { background-color:#CCCCCC; border-top:1px #000000 solid; border-right:1px #000000 solid; border-bottom:1px #000000 solid; border-left:1px #000000 solid; padding:2px; writing-mode:tb-rl;filter: flipv fliph; text-align:center; text-transform:capitalize; text-align:center; font-family:helvetica; font-weight:bold; font-size:8pt; } .cellHeaderVertTB { background-color:#CCCCCC; border-top:1px #000000 solid; border-right:1px #000000 solid; border-bottom:1px #000000 solid; border-left:1px #000000 solid; padding:2px; writing-mode:tb-rl;filter: flipv flipv; text-align:center; text-transform:capitalize; text-align:center; font-family:helvetica; font-weight:bold; font-size:8pt; } </style><body><div id=""reportBody""><p>Brad             ,             </p><p>The following individuals who report to you are able to log hours in CT+, but have not done so in a while.  Please remind them of the importance of logging hours as it saves Gentex money each year.</p><table><thead><tr><th>Employee</th><th>Last Entry</th></tr></thead><tbody><tr><td>Kevin Schueler</td><td>Jan 02, 2013</td></tr></tbody></table><p>Thanks!</p></div></body></html>"
  }
}
